package cn.myframe.controller.bus;

import cn.myframe.controller.AbstractController;
import cn.myframe.controller.BaseController;
import cn.myframe.entity.bus.BusCompanyEntity;
import cn.myframe.service.BaseService;
import cn.myframe.service.bus.BusCompanyService;
import cn.myframe.service.sys.SysRoleCompanyService;
import cn.myframe.service.sys.SysUserRoleService;
import cn.myframe.utils.ShiroUtils;
import cn.myframe.utils.annotation.CheckDelete;
import cn.myframe.utils.annotation.CheckNoPass;
import cn.myframe.utils.annotation.CheckSubmit;
import cn.myframe.utils.aop.AssociateData;
import cn.myframe.utils.core.R;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 存储运营商信息表
 * 
 * @author ynz
 * @email ynz@myframe.cn
 * @date 2018-08-23 15:01:45
 */
@RestController
@RequestMapping("/bus/buscompany")
public class BusCompanyController  extends AbstractController implements BaseController<BusCompanyEntity,BaseService<BusCompanyEntity>>{
	@Autowired
	private BusCompanyService busCompanyService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleCompanyService sysRoleCompanyService;

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping(value = "/list",method = {RequestMethod.GET})
	@RequiresPermissions("buscompany:list")
	public R list(@RequestParam Map<String, Object> params){
		return this.defaultList(params);
	}	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{companyNo}")
	@RequiresPermissions("buscompany:info")
	public R info(@PathVariable("companyNo") Long id){
		return this.defaultInfo(id,"busCompany");
	}
	
	/**
	 * 信息1
	 */
	@RequestMapping("/findone/{companyNo}")
	public R findone(@PathVariable("companyNo") Long id){
		BusCompanyEntity busCompany = busCompanyService.queryObject(id);
		return R.ok().put("busCompany", busCompany);
	}
	
	/**
	 * 新增保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("buscompany:save")
	public R save(@RequestBody BusCompanyEntity busCompany){		
		return this.defaultSave(busCompany);
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@CheckDelete(AssociateData.COMPANY_USER+","+AssociateData.COMPANY_NETWORK)
	@RequiresPermissions("buscompany:delete")
	public R delete(@RequestBody Long[] ids){		
		return this.defaultDelete(ids);
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")	
	@RequiresPermissions("buscompany:update")
	public R update(@RequestBody BusCompanyEntity busCompany){		
		return this.defaultUpdate(busCompany);
	}
	
   /**
    * 提交,审核
    */
	@RequestMapping("/audit")
	@CheckNoPass(AssociateData.COMPANY_USER+","+AssociateData.COMPANY_NETWORK)
	@CheckSubmit(AssociateData.COMPANY_REGION)
	@RequiresPermissions(value={"buscompany:submit","buscompany:auditpass","buscompany:auditnopass"},logical=Logical.OR)
	public R audit(@RequestBody Long[] ids ,@RequestParam Integer status){
		return this.defaultAudit(ids, status);
	}
	
   /**
	* 检验字段值重复
	*/
    @RequestMapping("/checkExist")
    @ResponseBody
    public String checkExist(String id,String value,String columnName){
        return this.defaultCheckExist(id, value, columnName);
    }
    
    public BusCompanyService getService(){
		return busCompanyService;
	}

	/**
	 * 树形列表
	 */
	@RequestMapping("/treelist")
	@RequiresPermissions("buscompany:list")
	public R treelist(@RequestParam Map<String, Object> params){
		List<BusCompanyEntity> busCompanyList = null;
		Long userId = ShiroUtils.getUserId();
		if (userId == 1L) {
			busCompanyList = busCompanyService.queryList(params);
		} else {
			List<Long> roleIds = sysUserRoleService.selectUserRoleIds(userId);
			Set<Long> companyIdSet = new HashSet<>();
			roleIds.forEach(x -> {
				List<Long> companyIds = sysRoleCompanyService.selectCompanyList(x);
				companyIds.forEach(y -> companyIdSet.add(y));
			});
			if (companyIdSet.size() > 0) {
				busCompanyList = busCompanyService.selectBatchIds(companyIdSet);
			}
		}
		return R.ok().put("companyList", busCompanyList);
	}
	
}
